package logservice

import (
	"fmt"
	lts "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/lts/v2"
	"github.com/huaweicloud/huaweicloud-sdk-go-v3/services/lts/v2/model"
	regionv2 "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/lts/v2/region"
)

type LogGroupGreeter interface {
	LogGroup(region string) LogGroupInterface
}

type LogGroupInterface interface {
	CreateLogGroup(groupName string) (id string, err error)
}

type logGroup struct {
	client *lts.LtsClient
	region string
}

func newLogGroup(log *LogService, region string) LogGroupInterface {
	_client := lts.NewLtsClient(
		lts.LtsClientBuilder().
			WithRegion(regionv2.ValueOf(region)).
			WithCredential(log.credentials).
			Build())
	return &logGroup{client: _client,
		region: region}
}

// CreateLogGroup 创建日志组
func (l *logGroup) CreateLogGroup(groupName string) (id string, err error) {
	request := &model.CreateLogGroupRequest{}
	request.Body = &model.CreateLogGroupParams{
		TtlInDays:    int32(3),
		LogGroupName: groupName,
	}
	response, err := l.client.CreateLogGroup(request)
	if err != nil {
		fmt.Printf("[LogService] 创建TLS日志组错误，%s", err)
		return
	}
	return *response.LogGroupId, err
}
